<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>
<%
	request.setAttribute("title", "充值");
%>
<%@ include file="/WEB-INF/views/shared/master/weContentHeader.jsp"%>
<style>
	.weui-cells{
		margin-top: 0px;
	}
	
	.rechargemoney>p{
		padding: 15px 10px;
		color: black;
		font-size: 16px;
	}
	.money{
		margin: 0px 10px;
		color: red;		
	}
	
	
	.color-blue {
		color: #2196F3;
	}
 	.bottom-btn {
		margin: 15px;
		padding: 2px;
		background-color: #2196F3;	
	} 
	
	.weui-btn_primary:not(.weui-btn_disabled):active{
		background-color: #2196F3;
	}
	
	.weui-btn_disabled{
	    background-color: #79c0f9 !important;
	    color:
	}
</style>

<%@ include file="/WEB-INF/views/shared/master/weTitle.jsp"%>


<div class="goback"
	data-back-href="<c:url value='/wechat/acc/myWealth/myWealth'/>"></div>
	
<input type="hidden" name="openId" id="openId" value="${customer.openId}" />
<input type="hidden" name="customerId" id="customerId" value="${customer.id}" />
<div class="rechargemoney">
	<p>充值金额（元）</p>
	
	<div class="weui-cell">
		<div class="weui-cell__hd">
			<span style="font-size: 25px;color: black;font-weight: 500;padding-right: 10px">￥</span>
		</div>
		<div class="weui-cell__bd">
			<input class="weui-input selected-color" id="money" type="number" maxLength="9"  placeholder="充值建议100元以上" style="font-size: 18px;" onkeyup="btnColor(),checkMoney()">
		</div>
	</div>
	<span class="info" style="color: #ea693f;margin-left: 20px" hidden></span>
	<p>可用余额
		<span class="money">
			<fmt:formatNumber value="${accountInfo.balance}" pattern="0.00"/> 
		</span>
			元
	</p>
</div>

<div style="width:100%;height:20px;background-color: #ebebeb;" >
  
</div>

 <div  style="z-index:500;">
	<a href="javascript:recharge();" data-href="<c:url value='/wechat/acc/myWealth/myWealth'/>" class="weui-btn weui-btn_primary bottom-btn recharge weui-btn_disabled" id="btn">充&nbsp;&nbsp;&nbsp;&nbsp;值</a>
</div>	 

<%@ include file="/WEB-INF/views/shared/scripts/jquery.jsp"%>
<%@ include file="/WEB-INF/views/shared/scripts/jqweui.jsp"%>
<%@ include file="/WEB-INF/views/shared/scripts/tabmob.jsp"%>

<%@ include file="/WEB-INF/views/shared/master/weContentFooter.jsp"%>

<script type="text/javascript">
   
   var $recharge = $(".recharge");
   var $money = $("#money");
   var value = '';
   //根据输入框是否有内容动态使按钮切换状态
   function btnColor(){
		 value =  $money.val();
		 if(value != ''){ 
			 if($recharge.attr("class").indexOf("weui-btn_disabled")>0){
				 $recharge.removeClass("weui-btn_disabled");
			 }
		 } 
		 if(value == ''||value ==0){ 
				 $("#btn").attr("disabled", true); 
				 $recharge.addClass("weui-btn_disabled");
		 } 
     } 
   
   /* 检查输入 */
	function checkInput() {
		if (checkMoney()) {
			return true;
		}
		return false;
	}
	/* 检查是否输入金额 */
	function checkMoney() {
		
		var balanceMoney = 999999999.99;
		var money = $("#money").val();
		var balance = ${accountInfo.balance};
		var reg = /(^[1-9](\d+)?(\.\d{1,2})?$)|(^(0){1}$)|(^\d\.\d{1,2}?$)/;
		/* var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|131(^[0-9]\.[0-9]([0-9])?$)/; */
		
		if (money == '' || money == 0) {
			$("#btn").attr("disabled", false);
			return false;
		} else {
			if((balance+parseInt(money)) >= balanceMoney){
				$("#btn").attr("disabled", true);
				$(".info").html("输入金额超限,您最多可以充值" + (balanceMoney-balance) + "元");
				$(".info").show();
				var $recharge = $(".recharge");
				$("#btn").attr("disabled", true);
				$recharge.addClass("weui-btn_disabled");
				return false;
			}else{
				if(!reg.test(money)){
					$("#btn").attr("disabled", true);
					$(".info").html("请输入正确的金额格式");
					$(".info").show();
					var $recharge = $(".recharge");
					$("#btn").attr("disabled", true);
					$recharge.addClass("weui-btn_disabled");
					return false;
				}
				$(".info").hide();
				return true;
			}
			$(".info").hide();
			return true;
		}
		return true;
	}

	//点击充值按钮要执行的操作
	function recharge() {
		if (!checkInput()) {
			return;
		}
		if (value != '') {
			var url = $.getVirtualPath() + "/wechat/acc/myWealth/payInfo";
			
			var result=$.getJsonPost(url,{
				money : value,
				openId:$("#openId").val()
			});
			startPay(result,payCallback);
		}
	}
	
	
	//微信充值
	function startPay(res,payCallback){
		if($.isNull(res)){
			$.alert("支付信息不能为空");
			return;
		}
		if($.isNull(payCallback)){
			$.alert("回调信息不能为空");
			return;
		}
		
		
		console.log(res);
		var config={
		           "appId":res.appId,     //公众号名称，由商户传入     
		           "timeStamp":""+res.timeStamp,         //时间戳，自1970年以来的秒数     
		           "nonceStr":res.nonceStr, //随机串     
		           "package":res.package,     
		           "signType":res.signType,         //微信签名方式：     
		           "paySign":res.paySign //微信签名 
		       };
		
		WeixinJSBridge.invoke('getBrandWCPayRequest', config,payCallback); 
	}
	
	function payCallback(res){
		
		if(res.err_msg == "get_brand_wcpay_request:ok") {
     	   	$.toast("支付成功");
     	   	var url = $.getVirtualPath()+"/wechat/acc/myWealth/rechargeResult";
     	   	var customerId = $("#customerId").val();
     	   	var money = $money.val();
     	   	
     	   	$.post(url,{customerId:customerId,money:money,outTradeNo:res.outTradeNo},function(result){
     	   		if(result=="true"){
	 		   		setTimeout(function(){
	 			   		window.location.href=$(".recharge").data("href");	
	 		   		},300);
	 	   		} 
     	   	})
        }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
     	   	$.toast("支付取消","cancel");
        }else if(res.err_msg == "get_brand_wcpay_request:fail"){
     	   	$.toast("支付失败", "forbidden");
        }  
	}
</script>





